package com.lora.judge.model;

import com.google.common.collect.Maps;
import com.lora.judge.CheckModel;
import com.lora.judge.CheckUtil;
import lombok.Data;
import org.apache.commons.lang3.StringUtils;

import java.math.BigDecimal;
import java.util.Date;
import java.util.Map;

@Data
public class CheckItem<T> {
    private static Map<String, String> formatter = Maps.newTreeMap();
    private String filedName;
    private String filedTitle;
    private T standard;
    private T checkObject;
    private String checkAlgorithm;
    private String equipmentName;
    private String customTip; // 自定义提示
    private T begin;
    private T end;
    static {
        formatter.put("GT", "%s[%s]预警[%s]值大于了预警值[%s]");
        formatter.put("LT", "%s[%s]预警[%s]值小于了预警值[%s]");
        formatter.put("GTE", "%s[%s]预警[%s]值大于等于了预警值[%s]");
        formatter.put("LTE", "%s[%s]预警[%s]小大于等于了预警值[%s]");
        formatter.put("EQ", "%s[%s]预警[%s]等于了预警值[%s]");
    }
    public String check() {
        String resultFormatter = formatter.get(this.checkAlgorithm);
        CheckModel checkModel = CheckUtil.getCheck(this.checkAlgorithm);
        checkModel.init(standard, checkObject, begin, end);
        boolean checkResult;
        if(checkObject instanceof BigDecimal) {
            checkResult = checkModel.checkBigDecimal();
            if(!checkResult) {
                if(StringUtils.isNotBlank(customTip)) {
                    return customTip;
                } else {
                    resultFormatter = String.format(resultFormatter,
                            equipmentName,
                            filedTitle,
                            ((BigDecimal) checkObject).doubleValue(),
                            ((BigDecimal) standard).doubleValue());
                    return resultFormatter;
                }
            }
        } else if(checkObject instanceof Double) {

        } else if(checkObject instanceof Integer) {

        } else if(checkObject instanceof Long) {

        } else if(checkObject instanceof String) {
            checkResult = checkModel.checkString();
            if(!checkResult) {
                if(StringUtils.isNotBlank(customTip)) {
                    return customTip;
                } else {
                    checkResult = checkModel.checkString();
                    if(!checkResult) {
                        resultFormatter = String.format(resultFormatter,
                                equipmentName,
                                filedTitle,
                                checkObject,
                                standard);
                        return resultFormatter;
                    }
                }
            }
        } else if(checkObject instanceof Date) {

        } else if(checkObject instanceof Short) {

        }
        return null;
    }

}
